package com.ly.spring.jdbc.dao;


import com.ly.spring.jdbc.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AccountDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    public List<Account> query(){
        String sql = "select * from bank_account";

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Account>(Account.class));
    }

    public Account queryById(long id){
        String sql = "select * from bank_account where id=?";

        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class),id);
    }

    public void insert(long id,String password, double balance){
        String sql = "insert into bank_account values(?,?,?)";
        jdbcTemplate.update(sql,id,password,balance);
    }

    public void updateBalanceById(long id,double money){
        String sql = "update bank_account set balance=balance + ? where id=?";
        jdbcTemplate.update(sql,money,id);
    }
}
